package mainpackage;

//import com.nokia.mid.sound.Sound;
//import com.nokia.mid.ui.DeviceControl;
//Wave audio files: audio/x-wav
//AU audio files: audio/basic
//MP3 audio files: audio/mpeg
//MIDI files: audio/midi
//Tone sequences: audio/x-tone-seq
import javax.microedition.media.*;
import java.io.*;

public final class SoundPlayerUtil {
//  static String[] type = {
//      "audio/midi", "audio/midi", "audio/midi",
//      "audio/midi", "audio/x-wav", "audio/x-wav", "audio/x-wav", /*      "audio/x-wav",
//      "audio/x-wav", "audio/x-wav", "audio/x-wav", "audio/x-wav", */"audio/x-wav",
//      "audio/x-wav", "audio/x-wav", "audio/x-wav", "audio/x-wav", "audio/x-wav"};

    static String[] name = {
        "xinshou.mid", "shengyu.mid", "zhanchang.mid",};
    static String[] type = {
        "audio/midi", "audio/midi", "audio/midi",};
    static private Player[] sndPlayers = new Player[3];
//  static private Player[] sndPlayers = new Player[18];
    static int soundIsOpen = 0, vibraIsOpen = 0; //�Ƿ��Ƿ���

    public SoundPlayerUtil() { //
        for (int i = 0; i < sndPlayers.length; i++) {
            InputStream is = getClass().getResourceAsStream("/res/music/" + name[i]);
            try {
//        sndPlayers[i].realize();
                sndPlayers[i] = Manager.createPlayer(is, type[i]);
                sndPlayers[i].realize();
                sndPlayers[i].prefetch();
                if (i == 1) {
                    sndPlayers[i].setLoopCount(-1);
                } else {
                    sndPlayers[i].setLoopCount(1);
                }
            } catch (Exception ex) {
                 if (Constant.DEBUG) 
                ex.printStackTrace();
            }
        }
    }

    public static void playmusic(int currentPlay) {

//    Tools.printLog("start music");
//    if (MainCanvas.music) {
//      Tools.printLog("dddddddd");
        stopAllMusic(currentPlay);
        try {
            if (sndPlayers[currentPlay].getState() != Player.STARTED) {
                sndPlayers[currentPlay].setMediaTime(0);
                sndPlayers[currentPlay].start();
            }
        } catch (MediaException me) {
             if (Constant.DEBUG) 
            me.printStackTrace();
        } catch (Exception e) {
             if (Constant.DEBUG) 
            e.printStackTrace();
        }
//    }
    }

    public static final void stopMusic(int index) {

        try {
//      Tools.printLog("111");
            sndPlayers[index].stop();
        } catch (MediaException ex) {
             if (Constant.DEBUG) 
            ex.printStackTrace();
        }

    }

    public static final void stopAllMusic(int currentPlay) {
        for (int i = 0; i < sndPlayers.length; i++) {
            if (i != currentPlay) {
                try {
                    sndPlayers[i].stop();
                } catch (MediaException ex) {
                     if (Constant.DEBUG) 
                    ex.printStackTrace();
                }
            }
        }
    }

    public static final void stopAllMusic() {
        for (int i = 0; i < sndPlayers.length; i++) {
            try {
                sndPlayers[i].stop();
            } catch (MediaException ex) {
                 if (Constant.DEBUG) 
                ex.printStackTrace();
            }
        }
    }

//  public static final void playAllMusic() {
//   for (int i = 0; i < sndPlayers.length; i++) {
//       try {
//         sndPlayers[i].start();
//       }
//       catch (MediaException ex) {
//     }
//   }
// }
    public static final void quit() {
        for (int i = 0; i < sndPlayers.length; i++) {
            sndPlayers[i].close();
        }
    }
}
